package com.sunday.pinxinghui.model.lifecircle;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.imageselector.MultiImageSelectorActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ImageUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.utils.UploadUtils;
import com.sunday.common.widgets.NoScrollGridView;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.adapter.GradImageAdapter;
import com.sunday.pinxinghui.base.BaseApplication;
import com.sunday.pinxinghui.entity.Images;
import com.sunday.pinxinghui.network.ApiClient;
import com.sunday.pinxinghui.widgets.MyRatingBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by 刘涛 on 2017/8/30.
 */

public class MakeCouponCommentActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.service_rating_bar)
    MyRatingBar serviceRatingBar;
    @Bind(R.id.edit_text)
    EditText editText;
    @Bind(R.id.grid_view)
    NoScrollGridView gridView;

    private int couponId;
    private long memberId;
    private String shopName;
    private GradImageAdapter adapter;
    private List<String> imgs = new ArrayList<>();
    private List<String> netImgs = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_coupon_comment);
        ButterKnife.bind(this);
        couponId = getIntent().getIntExtra("couponId", 0);
        shopName = getIntent().getStringExtra("shopName");
        memberId = BaseApplication.getInstance().getMember().getId();
        titleView.setText(shopName);
        serviceRatingBar.setRating(5, R.drawable.rating_bar, true, 30);
        adapter = new GradImageAdapter(5, imgs, 0);
        gridView.setAdapter(adapter);
        adapter.setOnClickListener(onClickListener);
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.photo_add:
                    if (imgs.size() == (int) v.getTag()) {
                        toImage(5 - imgs.size());
                    }
                    break;
                case R.id.photo_delete:
                    imgs.remove((int) v.getTag());
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    public void toImage(int max) {
        intent = new Intent(mContext, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, max);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
        startActivityForResult(intent, 1111);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1111:
                    ArrayList<String> mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                    for (String path : mSelectPath) {
                        imgs.add(ImageUtils.getCompressImagePath(path));
                    }
                    adapter.notifyDataSetChanged();
                    return;
            }
        }
    }

    @OnClick(R.id.btn_submit)
    void save() {
        String content = editText.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            ToastUtils.showToast(mContext, "请填写具体评价内容");
            return;
        }
        if (imgs.size() > 0) {
            UpImgae();
        } else {
            submitApply();
        }

    }

    private void UpImgae() {
        showLoadingDialog(0);
        Call<ResultDO<List<Images>>> call = ApiClient.getApiAdapter().saveImages(UploadUtils.getRequestBody(imgs));
        call.enqueue(new Callback<ResultDO<List<Images>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Images>>> call, Response<ResultDO<List<Images>>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    netImgs.clear();
                    for (Images images : response.body().getResult()) {
                        netImgs.add(images.getSaveUrl());
                    }
                    submitApply();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Images>>> call, Throwable t) {

            }
        });
    }

    private void submitApply() {
        String content = editText.getText().toString().trim();
        int rate = serviceRatingBar.getRating();
        showLoadingDialog(0);
        Call<ResultDO> call = ApiClient.getApiAdapter().couponComment(couponId, memberId, rate, content, StringUtils.listToString(netImgs));
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                dissMissDialog();
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "提交成功");
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


}
